
import sys
sys.path.append('..')
from python_util import util
import yaml

def prepareKeepalivedConfigFile():
    configYml = open(r'../config.yml')
    config = yaml.safe_load(configYml)
    isIngressProviderEnable = config['ingress-provider']['enable']
    print('ingress provider enable: ' +
          str(isIngressProviderEnable))
    if not isIngressProviderEnable:
        print('this node ingress provider not enable')
        sys.exit(0)

    networkInterfaceCard = config['network-interface-card']
    if networkInterfaceCard is None:
        print('network interface card is not config')
        sys.exit(1)

    # keepalived
    keepAlivedCfgFileName = 'config/keepalived.conf'
    keepAlivedCheckApiServerFileName = 'config/check_ingress_provider.sh'
    keepAlivedPriority = config['ingress-provider']['keepalived']['priority']
    keepAlivedVip = config['ingress-provider']['keepalived']['virtual-ipaddress']
    apiServerVip = config['ingress-provider']['keepalived']['check-ingress-provider']['ingress-provider-vip']
    apiServerDestPort = config['ingress-provider']['keepalived']['check-ingress-provider']['ingress-provider-dest-port']
    print('keep alived priority: ' + str(keepAlivedPriority))
    print('keep alived vip: ' + keepAlivedVip)
    print('keep alived api server vip: ' + apiServerVip)
    print('keep alived api server dest port: ' + str(apiServerDestPort))

    keepAlivedNetworkInterfaceCardSearchText = r"^(.*interface).*$"
    keepAlivedNetworkInterfaceCardReplaceText = r"\1 " + networkInterfaceCard
    util.replaceText(keepAlivedNetworkInterfaceCardSearchText,
                     keepAlivedNetworkInterfaceCardReplaceText, keepAlivedCfgFileName)

    keepAlivedPrioritySearchText = r"^(.*priority).*$"
    keepAlivedPriorityReplaceText = r"\1 " + str(keepAlivedPriority)
    util.replaceText(keepAlivedPrioritySearchText,
                     keepAlivedPriorityReplaceText, keepAlivedCfgFileName)
    masterKeepAlivedPriority = 101
    if keepAlivedPriority < 101:
        keepAlivedStateSearchText = r"^(.*state).*$"
        keepAlivedStateReplaceText = r"\1 " + "BACKUP"
        util.replaceText(keepAlivedStateSearchText,
                         keepAlivedStateReplaceText, keepAlivedCfgFileName)

    keepAlivedVipSearchText = r'''^(.*virtual_ipaddress {.*$)
    (^\s*).*(\s*$)'''
    keepAlivedVipReplaceText = r'''\1
    \g<2>''' + keepAlivedVip + r'''\3'''
    util.replaceText(keepAlivedVipSearchText,
                     keepAlivedVipReplaceText, keepAlivedCfgFileName)

    apiServerVipSearchText = r'''(^export INGRESS_PROVIDER_VIP=).+'''
    apiServerVipReplaceText = r'\g<1>' + apiServerVip
    util.replaceText(apiServerVipSearchText,
                     apiServerVipReplaceText, keepAlivedCheckApiServerFileName)

    apiServerDestPortSearchText = r'''(^export INGRESS_PROVIDER_DEST_PORT=)\d+'''
    apiServerDestPortReplaceText = r'\g<1>' + str(apiServerDestPort)
    util.replaceText(apiServerDestPortSearchText,
                     apiServerDestPortReplaceText, keepAlivedCheckApiServerFileName)
